<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');


class Users_model extends CI_Model
{
    
    public $user;
    private $CI;
    
    public function __construct()
    {
        parent::__construct();
        $this->CI =& get_instance();
        if ($this->session->userdata('user_id') != '') {
            $this->userLogin($this->session->userdata('user_id'));
        }
    }
    
    public function userLogin($user_id)
    {
        if (!$user = $this->getUser($user_id)) {
            return FALSE;
        }
        
        $this->user = $user;
        return TRUE;
    }
    
    public function logged_in()
    {
        if (empty($this->user)) {
            return false;
        } else {
            return true;
        }
    }
    
    public function genPassword($length = 5)
    {
        $this->load->helper('string');
        $makepass = random_string('numeric', $length);
        return $makepass;
    }
    
    public function hasPassword($password)
    {
        return md5($password . setting('encryption_key'));
    }
    
    public function isVip()
    {
        if ($this->logged_in()) {
            if (strtotime($this->user['day_out_vip']) > strtotime('now')) {
                return true;
            }
        } else {
            return false;
        }
    }

    public function addDay($username = '', $day = 0)
    {
        if ($day > 0 && $user = $this->getUser('', $username)) {
            $day_out_vip = toWeek($user['day_out_vip'], false, $day);
            $this->updateAccount($user['id'], $user['username'], $user['password'], $day_out_vip);
        } else {
            return false;
        }
        return true;
    }

    public function dayvip()
    {
        $a = strtotime($this->user['day_out_vip']);
        $b = strtotime('now');
        if ($this->isVip() && $a > $b) {
            return khoangcachngay(fdate($this->user['day_out_vip'], '%d/%m/%Y'), fdate('now', '%d/%m/%Y'));
        } else {
            return '0';
        }
    }

    public function login($username, $password)
    {
        $username = addslashes($username);
        $password = addslashes($password);
        $password = $this->hasPassword($password);
        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $this->db->where('is_active', 1);
        $result = $this->db->get('manager_users');
        if ($result->num_rows() > 0) {
            $user_db = $result->row_array();
            $this->session->set_userdata('user_id', $user_db['id']);
        } else {
            return false;
        }
        
        return true;
    }
    
    public function logout()
    {
        $this->session->unset_userdata('user_id');
        return true;
    }
    
    public function get($parameter = false)
    {
        if ($parameter) {
            return $this->user[$parameter];
        } else {
            return $this->user;
        }
    }
    
    public function getUser($user_id = '', $username = '')
    {
        if ($username != '') {
            $filters = array(
                'username' => $username
            );
        } elseif ($user_id != '') {
            $filters = array(
                'id' => $user_id
            );
        }
        $user = $this->getUsers($filters);
        
        if (empty($user)) {
            return false;
        } else {
            return $user[0];
        }
    }
    public function createAccount($username, $password, $day_out_vip = '')
    {
        if ($day_out_vip == '') {
            $day_out_vip = fdate('now');
        } else {
            $day_out_vip = toWeek('', false, $day_out_vip);
        }
        $fields = array(
            'username' => $username,
            'password' => $this->hasPassword($password),
            'value_group' => '1',
            'created' => fdate('now'),
            'is_active' => '1',
            'day_out_vip' => $day_out_vip,
            'value_token' => ''
        );
        $this->db->insert('manager_users', $fields);
        if (isset($this->CI->cache)) {
            $this->CI->cache->file->clean();
        }
        return $this->db->insert_id();
    }
    
    public function updateAccount($id, $username, $password, $day_out_vip)
    {
        $field = array(
            'username' => $username,
            'password' => $password,
            'day_out_vip' => $day_out_vip
        );
        $this->db->update('manager_users', $field, array(
            'id' => $id
        ));
        if (isset($this->CI->cache)) {
            $this->CI->cache->file->clean();
        }
        return TRUE;
    }
    public function Publish($id, $publish)
    {
        $field = array(
            'is_active' => $publish
        );
        $this->db->update('manager_users', $field, array(
            'id' => $id
        ));
        if (isset($this->CI->cache)) {
            $this->CI->cache->file->clean();
        }
        return TRUE;
    }
    public function deleteAccount($id)
    {
        $this->db->delete('manager_users', array(
            'id' => $id
        ));
        if (isset($this->CI->cache)) {
            $this->CI->cache->file->clean();
        }
        return true;
    }
    public function getCount($filters = array())
    {
        return $this->getUsers($filters, true);
    }
    public function getUsers($filters = array(), $count = false)
    {
        $this->db->from('manager_users');
        if (isset($filters['id'])) {
            $this->db->where('id', $filters['id']);
        }
        if (isset($filters['username'])) {
            $this->db->where('username', $filters['username']);
        }
        if (isset($filters['is_active'])) {
            $this->db->where('is_active', $filters['is_active']);
        }
        $result = $this->db->get();
        if ($count) {
            $rows = $result->num_rows();
            $result->free_result();
            return $rows;
        }
        if ($result->num_rows() == 0) {
            return false;
        }
        $users = array();
        foreach ($result->result_array() as $row) {
            $users[] = array(
                'id' => $row['id'],
                'username' => $row['username'],
                'password' => $row['password'],
                'group' => $row['value_group'],
                'created' => $row['created'],
                'is_active' => $row['is_active'],
                'day_out_vip' => $row['day_out_vip'],
                'token' => $row['value_token']
            );
        }
        
        $result->free_result();
        return $users;
    }
    
}